package com.example.hadoop.mapreduce.ordermaxamount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/28/12:26
 * @Description:
 * 求每个订单中的最大金额的item
 * 订单重写compareTo方法，从大到小排序
 * 重写getPartitioner，具有相同的OrderID 则存入同一个分区
 *  --> 同一个得到的结果将会是
 *      <item1, 330>
 *      <item2,250>
 *  现在得到的key都不同，可以重写GroupingComparator，告知reducer什么情况下作为同一组(这个做成orderId相同的Key视为相同)
 */
public class OrderMaxAmount {

    static class OrderMaxAmountMapper extends Mapper<LongWritable, Text, OrderItemBean, NullWritable> {
        OrderItemBean itemBean = new OrderItemBean();
        DoubleWritable amount = new DoubleWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String itemInfo = value.toString();
            String[] itemInfos = itemInfo.split("\t");
            itemBean.setItemId(itemInfos[0]);
            itemBean.setOrderId(itemInfos[1]);
            amount.set(Double.parseDouble(itemInfos[2]));
            itemBean.setAmount(amount);
            context.write(itemBean, NullWritable.get());
        }
    }


    /**
     * partitioner
     * 同一个orderId下的item作为一个分区
     */
    static class OrderMaxAmountPartitioner extends Partitioner<OrderItemBean, NullWritable> {

        @Override
        public int getPartition(OrderItemBean orderItemBean, NullWritable nullWritable, int numPartitions) {
            return (orderItemBean.getOrderId().hashCode() & Integer.MAX_VALUE) % numPartitions;
        }
    }

    // grouping comparator

    /**
     * 利用reduce端的GroupingComparator来实现将一组bean看成相同的key
     */
    static class OrderMaxAmountGroupingComparator extends WritableComparator {
        protected OrderMaxAmountGroupingComparator() {
            super(OrderItemBean.class, true);
        }

        @Override
        public int compare(WritableComparable a, WritableComparable b) {
            OrderItemBean o1 = (OrderItemBean)a;
            OrderItemBean o2 = (OrderItemBean)b;
            //同在一个order下的假装看成同一个key,这里因为OrderItemBean里面有重写compareTo方法，按照了amount排序
            //所以拿第一个key就能拿到需求所要的最大金额
            return o1.getOrderId().compareTo(o2.getOrderId());
        }
    }

    //reducer
    static class OrderMaxAmountReducer extends Reducer<OrderItemBean, NullWritable, OrderItemBean, NullWritable> {
        @Override
        protected void reduce(OrderItemBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
            context.write(key, NullWritable.get());
        }
    }


    public static void main(String[] args) throws Exception {

        if (args == null || args.length < 2) {
            args[0] = "F:\\hadoop\\test\\ordermaxamount\\input";
            args[1] = "F:\\hadoop\\test\\ordermaxamount\\output";
        }
        Configuration conf = new Configuration();

        Job job = Job.getInstance();
        job.setJarByClass(OrderMaxAmount.class);
        job.setMapperClass(OrderMaxAmountMapper.class);
        job.setMapOutputKeyClass(OrderItemBean.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setPartitionerClass(OrderMaxAmountPartitioner.class);
        job.setGroupingComparatorClass(OrderMaxAmountGroupingComparator.class);
        job.setReducerClass(OrderMaxAmountReducer.class);

        job.setOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(OrderItemBean.class);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        Path outputPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(conf);
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        FileOutputFormat.setOutputPath(job, outputPath);

        boolean res = job.waitForCompletion(true);
        System.out.println("Successfully? --> " + res);
        System.exit(res ? 0  : 1);
    }
}
